class SessionsController < ApplicationController
  
  before_filter :ensure_login, :only => :destroy
 
  def index
    redirect_to(new_session_path)
  end
 
  def new
    @session = Session.new
    render :layout => 'authentification'
  end
 
  def create
    @session = Session.new(params[:session])
    if @session.save
      
      session[:id] = @session.id
      flash[:notice] = "Bienvenue #{@session.account.login}, vous êtes connecté!"
      
      target = session[:original_uri] || ''
      #session[:original_uri] = nil
      
      if session[:original_uri] != nil 
      redirect_to target
      else
      redirect_to(monitorings_url)
      end
      
    else
      session[:id] = nil
      render(:action => 'new')
    end
  end
 
  def destroy
    Session.destroy(@application_session)
    session[:id] = @user = nil
    session[:original_uri] = nil
    flash[:notice] = "Vous êtes déconnecté !"
    redirect_to(root_url)
  end
  
end
